$(function () {
    // 获取id
    var id = location.search.replace('?','').split('=')[1]
    //获取当前用户收获地址
    CT.loginAjax({
        url: '/address/queryAddress',
        type: 'get',
        data: '',
        dataType: 'json',
        success: function (data) {
            for(var i=0; i<data.length; i++) {
                if(data[i].id == id){
                    var result = data[i]
                }
            }
            $('.ct_content').html(template('editAddr', result))
        }
    })

    $('.ct_content').on('tap','button', function () {
        // var data = $.trim($('form ').serialize())
        // console.log(data)
        // var formData = CT.serialize2object(data)
        // console.log(formData)
        var recipients = $.trim($('[name="recipients"]').val())
        var address = $.trim($('[name="address"]').val())
        var postCode = $.trim($('[name="postCode"]').val())
        var addressDetail = $.trim($('[name="addressDetail"]').val())
        // 验证各项是否为空 =
        if (!recipients) {
            mui.toast('请输入姓名！')
            return false
        }
        if (!postCode) {
            mui.toast('请输入邮政编码！')
            return false
        }
        if (!address) {
            mui.toast('请输入省市区！')
            return false
        }
        if (!addressDetail) {
            mui.toast('请输入详细地址！')
            return false
        }
        CT.loginAjax({
            url: ' /address/updateAddress',
            type: 'post',
            data: {
                id: id,
                address: address,
                addressDetail: addressDetail,
                recipients: recipients,
                postcode: postCode
            },
            dataType: 'json',
            success: function () {
                location.href = 'address.html'
            },
            error: function () {
                mui.toast('网络繁忙！')
            }
        })
    })

})